﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZY.EntityFrameWork.Caller;
using ZY.EntityFrameWork.Caller.Facade;
using ZY.EntityFrameWork.Core.Model.Dto;
using ZY.EntityFrameWork.Core.Model.Entity;

namespace EF_CodeFist
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        BindingList<ArchiveInfoDto> infos = new BindingList<ArchiveInfoDto>();

        private void Form1_Load(object sender, EventArgs e)
        {
            cbxCondition.Items.Add("ArvID");
            cbxCondition.SelectedIndex = 0;
            cbxQueryWay.SelectedIndex = 0;
            //ArchiveBLL bll = new ArchiveBLL();
            //bll.GetAll();
            //Model1 m = new Model1();
            //this.gridControl1.DataSource = m.FieldTypeInfo.ToList();
            //FieldTypeBLL bll = new FieldTypeBLL();
            //ArchiveBLL bll = new ArchiveBLL();
            //infos = new BindingList<ArchiveInfo>(BLLFactory<ArchiveBLL>.Instance.GetQueryable().ToList());
            
            //infos = new BindingList<ArchiveInfo>(BLLFactory<ArchiveBLL>.Instance.GetAll());
            List<ArchiveInfoDto> qInfos = CallerFactory.Instance.GetService<IArvOpService>().GetArvInLib();
            //ArvOPService op = new ArvOPService();
            //List<ArchiveInfoDto> qInfos = op.GetArvInLib();
            if (qInfos != null)
                infos = new BindingList<ArchiveInfoDto>(qInfos.ToList());
            this.gc.DataSource = infos;
        }

        private void toolAdd_Click(object sender, EventArgs e)
        {
            // 表格中新增一行可编辑行
            gvUser.AddNewRow();
            gvUser.OptionsBehavior.Editable = true;  // 行可编辑
            gvUser.ShowEditor();                     // 显示光标
            toolSave.Enabled = true;
            this.toolStrip1.Tag = "Add";
        }

        private void toolSave_Click(object sender, EventArgs e)
        {
            // 更新关联数据源
            gvUser.PostEditor();
            // 不用按回车或者点击另一个单元格就能保存编辑中的单元格内容
            gvUser.CloseEditor();

            if (!gvUser.UpdateCurrentRow()) return;

            // 表格禁止编辑
            gvUser.OptionsBehavior.Editable = false;
            // 获取当前列信息
            ArchiveInfoDto info = (ArchiveInfoDto)gvUser.GetFocusedRow();
            if (toolStrip1.Tag.ToString() == "Add")
            {
                ArvLocationDto loc = new ArvLocationDto
                {
                    //ArvID = info.ArvID,
                    GroupNo = "1",
                    LayerNo = "1"
                };
                //int r = BLLFactory<ArvOperationBLL>.Instance.InputToLib(info);
                int r = CallerFactory.Instance.GetService<IArvOpService>().InputToLib(info);
                if (r > 0)
                {
                    MessageBox.Show("入库成功");
                }
                else
                {
                    MessageBox.Show("入库失败");
                }

                //    r = BLLFactory<ArvOperationBLL>.Instance.InputToCab(info, loc);
                //    if (r > 0)
                //    {
                //        MessageBox.Show("入柜成功");
                //    }
                //    else
                //    {
                //        MessageBox.Show("入柜失败");
                //    }

                //    LendInfoDto lendInfo = new LendInfoDto { Lender = "aaa" };
                //    int[] selected=gvUser.GetSelectedRows();
                //    List<ArchiveInfoDto> lendArvs = new List<ArchiveInfoDto>();
                //    foreach(int i in selected)
                //    {
                //        lendArvs.Add((ArchiveInfoDto)(gvUser.GetRow(i)));
                //    }
                //    int r1 = BLLFactory<ArvOperationBLL>.Instance.ArvLend(lendInfo, lendArvs);
                //    if (r1 > 0)
                //    {
                //        MessageBox.Show("借阅成功");
                //    }
                //    else
                //    {
                //        MessageBox.Show("借阅失败");
                //    }
                //}
                //else if (toolStrip1.Tag.ToString() == "Edit")
                //{
                //    //BLLFactory<ArchiveBLL>.Instance.Update(info);
                //}


            }
        }

        private void toolEdit_Click(object sender, EventArgs e)
        {
            toolSave.Enabled = true;
            // 使能表格可编辑
            gvUser.OptionsBehavior.Editable = true;
            gvUser.ShowEditor();
            this.toolStrip1.Tag = "Edit";
        }

        private void toolDelete_Click(object sender, EventArgs e)
        {
            // 是否选中条目
            int[] selected = gvUser.GetSelectedRows();
            if (selected.Length == 0)
            {
                //MessageUtil.ShowWarning("请选择要删除的用户信息！");
                MessageBox.Show("请选择要删除的信息！");
                return;
            }

            // 表格选中的记录
            ArchiveInfo item = (ArchiveInfo)gvUser.GetFocusedRow();

            string str = string.Format("确实要删除该信息吗？");
            if (DialogResult.OK == MessageBox.Show(str, "警告信息", MessageBoxButtons.OK, MessageBoxIcon.Warning))
            {
                try
                {
                    List<ArchiveInfo> deleteList = new List<ArchiveInfo>();
                    foreach (int rowIndex in selected)
                    {
                        // 表格中删除选中的列
                        ArchiveInfo info = (ArchiveInfo)gvUser.GetRow(rowIndex);
                        deleteList.Add(info);
                        //gvUser.DeleteRow(rowIndex);
                    }
                    //BLLFactory<ArchiveBLL>.Instance.Delete(deleteList);
                    gvUser.DeleteSelectedRows();
                    MessageBox.Show("删除成功！");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void toolAllQuery_Click(object sender, EventArgs e)
        {
            if (cbxQueryWay.SelectedText == "精确查询")
            {
                //gridControl1.DataSource = BLLFactory<ArchiveBLL>.Instance.Find(q => q.ArvID==txtKeyWord.Text);
            }
            else
            {
                //gridControl1.DataSource = BLLFactory<ArchiveBLL>.Instance.Find(q => q.ArvID.Contains(txtKeyWord.Text));
            }
        }
    }
}
